<?php
namespace Admin\Controller;
use Think\Controller;
class AdminUserController extends Controller {
    //TP构造函数
    public function _initialize(){
        //自动加载的类
        new \Org\Admin_autoload();
        //当前页仅超级管理员可以访问
        if($this->admin_info['admin_group_id']>1){
            echo "<script>alert('当前页面仅超级管理员可以访问，请您使用超级管理员账号登录');history.back(-1);</script>";exit;
        }
    }
    //用户管理
    public function index(){
        //echo 82929;exit;
        $data['title']='后台用户管理_后台用户管理_找金银网';
        if(I('path.2')!=''){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        $Model=D("Admin");
        //搜索 关键词
        if(I('post.keyword')){
            $Model->keyword=I('post.keyword');
        }
        //搜索 分类
        /*if(I('post.pc_id')){
            $Model->pc_id=I('post.pc_id');
        }*/
        $Model->page=$page;
        $Model->perpage=$perpage;
        $user_list=$Model->get_user_list();
        //print_r($user_list);exit;
        $user_total=$Model->get_user_total();
        //echo $user_total;exit;
        $page_total=ceil($user_total/$perpage);
        //echo $page_total;exit;
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['perpage']=$perpage;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['user_total']=$user_total;
        $pager['page_total']=$page_total;
        //$cat_list=$Model->get_cat_list();
        $this->assign("user_list", $user_list);
        //$this->assign("cat_list", $cat_list);
        $this->assign("pager",$pager);
        $this->assign("data", $data);
        $this->display();
    }

    //编辑用户
    public function user_edit(){
        $data['title']='编辑后台用户 会员 - 找金银网';
        $admin_id=I('path.2');
        $Model=D("Admin");
        $Model->admin_id=$admin_id;
        $admin_info=$Model->get_admin_info();
        $admin_group_list=M('AdminGroup')->select();
        //print_r($admin_group_list);exit;
        //print_r($admin_info);exit;
        $this->assign("data", $data);
        $this->assign("admin_info", $admin_info);
        $this->assign("admin_group_list", $admin_group_list);
        $this->display();
    }

    //编辑提交
    public function user_edit_post(){
        if($_POST['admin_id']!=''){
            $post=I('post.');
            if($_POST['new_admin_passwd']){
                $admin_passwd=I('post.new_admin_passwd');
                $post['admin_passwd']=md5($admin_passwd);
            }
            $post['admin_group_id']=I('post.admin_group_id');
            //echo $post['admin_passwd'];exit;
            $Model=D("Admin");
            $Model->post=$post;
            if(!$Model->user_edit_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/admin/admin_user/index';</script>";
        }else{
            echo "<script>alert('请您提交完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
    }

    //
    public function user_add(){
        $admin_group_list=M('AdminGroup')->select();
        $data['title']='添加后台用户 会员 - 找金银网';
        $this->assign("data", $data);
        $this->assign("admin_group_list", $admin_group_list);
        $this->display();
    }

    //添加用户 提交
    public function user_add_post(){
        if($_POST['admin_name']==''||$_POST['admin_passwd']==''||$_POST['re_admin_passwd']==''){
            echo "<script>alert('请您填写完整后尝试重新提交');history.back(-1);</script>";exit;
        }
        if($_POST['re_admin_passwd']!=$_POST['admin_passwd']){
            echo "<script>alert('两次输入的密码不一致，请您尝试重新输入或者联系客服人员');history.back(-1);</script>";exit;
        }
        $post=I('post.');
        $post['admin_passwd']=md5(I('post.admin_passwd'));
        //print_r($post);exit;
        $Model=D("Admin");
        $Model->post=$post;
        if(!$Model->user_add_post()){
            echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/admin_user/index';</script>";
    }

    //用户组管理
    public function group(){
        $data['title']='后台用户组管理_后台用户管理_找金银网';
        if(I('path.2')!=''){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        $Model=D("AdminGroup");
        //搜索 关键词
        if(I('post.keyword')){
            $Model->keyword=I('post.keyword');
        }
        //搜索 分类
        /*if(I('post.pc_id')){
            $Model->pc_id=I('post.pc_id');
        }*/
        $Model->page=$page;
        $Model->perpage=$perpage;
        $group_list=$Model->get_group_list();
        //print_r($group_list);exit;
        $group_total=$Model->get_group_total();
        //echo $group_total;exit;
        $page_total=ceil($group_total/$perpage);
        //echo $page_total;exit;
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['perpage']=$perpage;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['group_total']=$group_total;
        $pager['page_total']=$page_total;
        //$cat_list=$Model->get_cat_list();
        $this->assign("group_list", $group_list);
        //$this->assign("cat_list", $cat_list);
        $this->assign("pager",$pager);
        $this->assign("data", $data);
        $this->display();
    }

    //编辑用户
    public function group_edit(){
        $data['title']='编辑后台用户 会员 - 找金银网';
        $admin_group_id=I('path.2');
        $Model=D("AdminGroup");
        $Model->admin_group_id=$admin_group_id;
        $admin_group_info=$Model->get_admin_group_info();
        $this->assign("data", $data);
        $this->assign("admin_group_info", $admin_group_info);
        $this->display();
    }

    //编辑提交
    public function group_edit_post(){
        if($_POST['admin_group_id']!=''){
            $post=I('post.');
            $Model=D("AdminGroup");
            $Model->post=$post;
            //print_r($post);exit;
            if(!$Model->admin_group_edit_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/admin/admin_user/group';</script>";
        }else{
            echo "<script>alert('请您提交完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
    }

    //添加用户组
    public function group_add(){
        $admin_group_list=M('AdminGroup')->select();
        $data['title']='添加后台用户 会员 - 找金银网';
        $this->assign("data", $data);
        $this->assign("admin_group_list", $admin_group_list);
        $this->display();
    }

    //添加用户组 提交
    public function admin_group_add_post(){
        $post=I('post.');
        //print_r($post);exit;
        $Model=D("AdminGroup");
        $Model->post=$post;
        if($Model->check_admin_group_exist()){
            echo "<script>alert('此用户组名称已经存在，请您尝试重新输入或者联系客服人员');history.back(-1);</script>";exit;
        }
        if(!$Model->admin_group_add_post()){
            echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/admin_user/group';</script>";
    }

}